/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_DEVICE_H__
#define __EB_DEVICE_H__

#include <kernel/initcall.h>
#include <kernel/kobject.h>


/**
 * @brief device struct, to describe device information
 * @name: device name
 * @kobj: kernel object
 * @driver: device driver
 * @driver_data: device driver data
 * @platform_data: device platform data
 * @bus: device bus
 * @compatible: device compatible, to match driver
 * @dt_node: device tree node
 */
struct device {
    const char      *name;
    struct kobj     kobj;
    struct driver   *driver;
    void            *driver_data;
    const void      *platform_data;
    struct bus_type *bus;
    const char      *compatible;
    struct fdt_node *dt_node;
};


#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief check whether the device is registered
 * @param dev device pointer
 * @return true or false, true: registered, false: unregistered
 */
bool device_is_registered(struct device *dev);


/**
 * @brief get device name
 * @param dev device pointer
 * @return device name
 */
const char *device_get_name(struct device *dev);


/**
 * @brief set device name
 * @param dev device pointer
 * @param name device name
 * @return none
 */
void device_set_name(struct device *dev, const char *name);


/**
 * @brief register device
 * @param dev device pointer
 * @return int, 0: success, -1: failed
 */
int device_register(struct device *dev);


/**
 * @brief unregister device
 * @param dev device pointer
 * @return none
 */
void device_unregister(struct device *dev);


/**
 * @brief create device attribute file, it will return a kobj
 * @param dev device pointer
 * @param name attribute name
 * @param data attribute data
 * @param len attribute data length
 * @return kobject pointer, NULL means failed
 */
#define device_create_attr(dev, name, data, len)  \
                    kobj_create_attr(&dev->kobj, name, data, len)


/**
 * @brief create device text attribute file, it will return a kobj
 * @param dev device pointer
 * @param name attribute name
 * @param text attribute text
 * @return kobject pointer, NULL means failed
 */
#define device_create_text_attr(dev, name, text)  \
                    kobj_create_text_attr(&dev->kobj, name, text);


#ifdef __cplusplus
}
#endif


/**
 * @brief set device driver data
 * @param dev device pointer
 * @param data device driver data
 * @return none
 */
static inline void device_set_drvdata(struct device *dev, void *data)
{
    dev->driver_data = data;
}


/**
 * @brief get device driver data
 * @param dev device pointer
 * @return device driver data
 */
static inline void *device_get_drvdata(struct device *dev)
{
    return dev->driver_data;
}


/**
 * @brief set device platform data
 * @param dev device pointer
 * @param data device platform data
 * @return none
 */
static inline void device_set_platform_data(struct device *dev, const void *data)
{
    dev->platform_data = data;
}


/**
 * @brief get device platform data
 * @param dev device pointer
 * @return device platform data
 */
static inline const void *device_get_platform_data(struct device *dev)
{
    return dev->platform_data;
}


#endif //!__EB_DEVICE_H__